<?php

namespace falcon5\inc\Infrastructure\Services;

use falcon5\inc\Base\Common;
use falcon5\inc\Base\ProxyHeader;
use falcon5\inc\Base\WPOption;
use falcon5\inc\Exceptions\RemoteServiceException;
use falcon5\inc\Infrastructure\Clients\FC5Client;
use falcon5\inc\Infrastructure\Services\Falcon5\AdminTrait;
use falcon5\inc\Infrastructure\Services\Falcon5\AuthTrait;
use falcon5\inc\Infrastructure\Services\Falcon5\CacheTrait;
use falcon5\inc\Infrastructure\Services\Falcon5\EntryTrait;
use GuzzleHttp\RequestOptions;
use hollisho\httpclient\Feign\FeignClient;
use hollisho\httpclient\Feign\FeignClientFactory;
use lucatume\DI52\ContainerException;
use Phinx\Util\Util;

class Falcon5Service
{
    use AuthTrait;
    use CacheTrait;
    use EntryTrait;
    use AdminTrait;

    /**
     * @var FeignClient|null
     */
    private $client = null;


    /**
     * @return FC5Client
     * @throws ContainerException
     */
    private function getDefaultClient()
    {
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        /** @var FC5Client $feignClient */
        $feignClient = FeignClientFactory::create(FC5Client::class, [
            'headers' => [
                ProxyHeader::FC5_API_KEY => $falcon5Option->getApiKey()
            ],
            'base_uri' => Common::getPlatformProxyHost(),
            RequestOptions::VERIFY => false,
        ]);
        return $feignClient;
    }
    private function getEngineDefaultClient()
    {
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        /** @var FC5Client $feignClient */
        $feignClient = FeignClientFactory::create(FC5Client::class, [
            'headers' => [
                ProxyHeader::FC5_API_KEY => $falcon5Option->getApiKey()
            ],
            'base_uri' => Common::getEngineProxyHost(),
            RequestOptions::VERIFY => false,
        ]);
        return $feignClient;
    }
    /**
     * @return FC5Client
     * @throws ContainerException
     */
    public function getClient()
    {
        if (!$this->client) {
            $this->client = $this->getDefaultClient();
        }

        return $this->client;
    }
    public function getClientEngine()
    {
        if (!$this->client) {
            $this->client = $this->getEngineDefaultClient();
        }

        return $this->client;
    }
    /**
     * @param $client
     */
    public function setClient($client)
    {
        $this->client = $client;
    }


    /**
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function supportLang()
    {

        //$response = $this->getClientEngine()->supportLang();
        $response = $this->getDefaultClient()->supportLang();
        $content = $response->getBody()->getContents();


        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }

    /**
     * @param string $pageIds
     * @param int $page
     * @param int $pageSize
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function getPageStatisticsList(string $pageIds = '', int $page = 1, int $pageSize = 10)
    {
        $response = $this->getClient()->getPageStatisticsList($pageIds, $page, $pageSize);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }


    /**
     * @param string $url
     * @param string $translatedLang
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function translateUrl(string $url, string $translatedLang)
    {
        $body = [
            'url' => $url,
            'translated_lang' => $translatedLang,
        ];
        $response = $this->getClient()->translateUrl($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }


    /**
     * @param $body
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function updateSite($body)
    {

        $response = $this->getClient()->updateSite($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }
    /**
     * @param $body
     * @return mixed
     * @throws RemoteServiceException
     */
    function getPlatformSubscribe($api_key){
        $body = [
            'api_key' => $api_key,
        ];
        $response = $this->getClient()->getPlatformSubscribe($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }
    
}
